using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Publicuse.Application.system;
using Publicuse.Autofac;
using Publicuse.Cache;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Util;

namespace Publicuse.Appication
{
  public class WhitesinglenameApplication
  {
    private static BaesApplication baseApplication;
    private static WhitesinglenameApplication userApplication;

    public static WhitesinglenameApplication GetApplication(ServiceContainer container)
    {
      baseApplication = new BaesApplication(container);
      return userApplication;
    }

    static WhitesinglenameApplication()
    {
      userApplication = new WhitesinglenameApplication();
    }

    /// <summary>
    /// 获取黑白名单详细信息
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    public async Task<CommonResult> GetDetail(int Id)
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        var list = baseApplication.mySqlSugar.QueryByExpression<Whitesinglename>(
          o => o.Id == Id,
          out var msg
        );
        if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
        {
          var spconf = list.FirstOrDefault();
          result.data = spconf;
        }
        result.msg = msg;
        return result;
      });
    }

    /// <summary>
    /// 获取黑白名单管理模块信息列表
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    public async Task<CommonResult> GetList(Whitesinglename query)
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        var tableName = TableAtributeUtil.GetTable<Whitesinglename>();
        string sql = query.GetQueryString<Whitesinglename>(null);
        WhitesinglenameQuery whitesinglenameQuery = new WhitesinglenameQuery()
        {
          tableName = tableName,
          sqlWhere = sql,
          sortFile = query.sortFile,
          sortType = query.sortType,
          files = query.files,
          pageIndex = query.pageIndex,
          pageSize = query.pageSize,
        };
        result = baseApplication
          .GetList<Whitesinglename>(whitesinglenameQuery, false)
          .GetAwaiter()
          .GetResult();
        return result;
      });
    }

    /// <summary>
    /// 批量审核数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<CommonResult> ConfirmAll(string data, string loginUserCode)
    {
      return await baseApplication.ConfirmAll<Whitesinglename>(
        BusinessType.whitesinglename,
        BusinessActionType.check,
        BusinessLogType.writeLogToDataBase,
        data,
        loginUserCode
      );
    }

    /// <summary>
    /// 审核数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<CommonResult> Confirm(string id, int type, string loginUserCode)
    {
      return await baseApplication.Confirm<Whitesinglename>(
        BusinessType.whitesinglename,
        BusinessActionType.check,
        BusinessLogType.writeLogToDataBase,
        id,
        type,
        loginUserCode
      );
    }

    /// <summary>
    /// 检查黑白名单管理模块IP地址是否存在
    /// </summary>
    /// <param name="IPAddress"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    public async Task<CommonResult> CheckIPAddress(
      string IPAddress,
      string loginUserCode,
      string Id = ""
    )
    {
      return await baseApplication.CheckDataExists<Whitesinglename>(
        BusinessType.whitesinglename,
        BusinessLogType.writeLogToDataBase,
        o => o.IPAddress == IPAddress && o.status != -9,
        "检查黑白名单管理模块IP地址是否存在",
        Id
      );
    }

    /// <summary>
    /// 检查黑白名单管理模块IP编码是否存在
    /// </summary>
    /// <param name="IPCode"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    public async Task<CommonResult> CheckIPCode(string IPCode, string loginUserCode, string Id = "")
    {
      return await baseApplication.CheckDataExists<Whitesinglename>(
        BusinessType.whitesinglename,
        BusinessLogType.writeLogToDataBase,
        o => o.IPCode == IPCode && o.status != -9,
        "检查黑白名单管理模块IP编码是否存在",
        Id
      );
    }

    /// <summary>
    /// 黑白名单管理信息导入
    /// </summary>
    /// <returns></returns>
    public async Task<CommonResult> ImportExcel(
      IFormFileCollection files,
      string path,
      string loginUserCode
    )
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult commonResult = new CommonResult();
        StringBuilder result = new StringBuilder();
        baseApplication.autofaceInstance.DoWork(
          BusinessType.whitesinglename,
          BusinessActionType.import,
          files[0].FileName,
          "黑白名单管理数据",
          BusinessLogType.writeLogToDataBase,
          (o) =>
          {
            commonResult = AddExcelToData(files, path);
            return commonResult;
          },
          null,
          ref commonResult
        );
        return commonResult;
      });
    }

    /// <summary>
    /// 从Excel中导入数据到数据库表
    /// </summary>
    private static CommonResult AddExcelToData(IFormFileCollection files, string path)
    {
      StringBuilder result = new StringBuilder();
      CommonResult commonResult = new CommonResult();
      string msg = string.Empty;
      ImportCache.InitImportData(
        files,
        path,
        "黑白名单管理",
        out msg,
        (list) =>
        {
          var module = "黑白名单管理";
          Dictionary<int, List<int>> aclist = new Dictionary<int, List<int>>();
          foreach (var dt in list) { }
          commonResult.data = "导入完成";
          return true;
        }
      );
      if (!string.IsNullOrEmpty(msg))
      {
        commonResult.msg = "请选择黑白名单管理导入";
        commonResult.data = null;
        commonResult.count = 0;
      }
      return commonResult;
    }

    /// <summary>
    /// 新增修改黑白名单管理模块信息
    /// </summary>
    /// <param name="Contacts"></param>
    /// <returns></returns>
    public async Task<CommonResult> AddOrUpdate(Whitesinglename info)
    {
      return await baseApplication.AddOrUpdate<Whitesinglename>(
        info,
        BusinessType.whitesinglename,
        BusinessLogType.writeLogToDataBase
      );
    }

    /// <summary>
    /// 删除黑白名单管理模块信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public async Task<CommonResult> Delete(string Id, string loginUserCode, string type)
    {
      return await baseApplication.Delete<Whitesinglename>(
        Id,
        loginUserCode,
        type,
        BusinessType.whitesinglename,
        (a, b, info) =>
        {
          return info.Id.ToString();
        }
      );
    }

    /// <summary>
    /// 还原黑白名单管理模块信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    public async Task<CommonResult> Reduction(string Id, string loginUserCode)
    {
      return await baseApplication.Reduction<Whitesinglename>(
        Id,
        loginUserCode,
        BusinessType.whitesinglename,
        (a, b, info) =>
        {
          return info.Id.ToString();
        }
      );
    }
  }
}
